#!/bin/bash

    if ! command -v java > /dev/null 2>&1; then
        echo "Java未安装，正在尝试安装..."
        if command -v pkg > /dev/null 2>&1; then
            echo "检测到支持的包管理器，正在安装Java..."
            sed -i 's@^\(deb.*stable main\)$@#\1\ndeb https://mirrors.tuna.tsinghua.edu.cn/termux/apt/termux-main stable main@' $PREFIX/etc/apt/sources.list
            apt update && apt upgrade
            pkg i openjdk-17 -y
        else
            echo "请手动安装java"
            exit 1
        fi
    fi
    TAG_NAME=$(curl -Ls "https://api.github.com/repos/HiveGamesOSS/Chunker/releases/latest" | grep '"tag_name":' | sed -E 's/.*"([^"]+)".*/\1/')
    if [ ! -f chunker-cli-${TAG_NAME}.jar ]; then
        echo "下载相关程序..."
        curl -LO "https://github.com/HiveGamesOSS/Chunker/releases/download/${TAG_NAME}/chunker-cli-${TAG_NAME}.jar"
    fi
    if command -v termux-setup-storage > /dev/null 2>&1; then
        termux-setup-storage
    fi
echo "输入目录的路径，相对于应用所在位置。"
echo "输出目录的路径，相对于应用所在位置。"
echo "转换后的版本，你基岩版填B，JAVA版填J"
echo "转换后的游戏版本，也就是你转换后游玩的版本"
read -p "请输入输入目录的路径: " input
read -p "请输入输出目录的路径: " output
read -p "请输入转换后的版本[B/J]: " editionjc
if [ "$editionjc" = "B" ]; then
    edition="BEDROCK_"
else
    edition="JAVA_"
fi
read -p "转换后的游戏版本: " v
IFS='.' read -r -a parts <<< "$v"
if [[ ${#parts[2]} -ge 2 ]]; then
    third_part=$((parts[2] / 10 * 10))
else
    third_part=${parts[2]}
fi
version="${parts[0]}_${parts[1]}_${third_part}"
java -jar chunker-cli-${TAG_NAME}.jar -i "${input}" -f ${edition}${version} -o ${output}